package com.itmk.web.goods_order.controller;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itmk.annotation.Auth;
import com.itmk.utils.ResultUtils;
import com.itmk.utils.ResultVo;
import com.itmk.web.goods.entity.MyGoodsParm;
import com.itmk.web.goods.service.GoodsService;
import com.itmk.web.goods_order.entity.GoodsOrder;
import com.itmk.web.goods_order.entity.MyOrder;
import com.itmk.web.goods_order.entity.OrderParm;
import com.itmk.web.goods_order.entity.OrderVo;
import com.itmk.web.goods_order.service.GoodsOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author 山顶洞人郑某
 * @className GoodsOrderController.java
 * @date 2023 12 11
 */
@RestController
@RequestMapping("/api/goodsOrder")
@Slf4j
public class GoodsOrderController {
    @Autowired
    private GoodsOrderService goodsOrderService;
    @Autowired
    private GoodsService goodsService;
    //交易订单
    @PostMapping("/replaceOrder")
    public ResultVo replaceOrder(@RequestBody GoodsOrder order){
        goodsOrderService.replaceOrder(order);
        return ResultUtils.success("交易成功!");
    }


    //小程序我的订单
    @GetMapping("/getMyOrder")
    public ResultVo getMyOrder(MyGoodsParm parm){
        IPage<MyOrder> list = goodsService.getMyOrder(parm);
        return ResultUtils.success("查询成功！",list);
    }

    //我的出售订单
    @GetMapping("/getSellOrder")
    public ResultVo getSellOrder(MyGoodsParm parm){
        IPage<MyOrder> list = goodsService.getSellOrder(parm);
        return ResultUtils.success("查询成功！",list);
    }

    //删除订单
    @PostMapping("/deleteOrder")
    public ResultVo deleteOrder(@RequestBody GoodsOrder goodsOrder){
        if (goodsOrderService.removeById(goodsOrder.getOrderId())){
            return ResultUtils.success("删除成功");
        }
        return ResultUtils.error("No");
    }
    @CrossOrigin
    //订单列表
    @Auth
    @GetMapping("/getList")
    public ResultVo getList(OrderParm parm)  {
        Page<OrderVo> list = goodsOrderService.getList(parm);
        return ResultUtils.success("查询成功",list);
    }

    @PostMapping("/sendOrder")
    public ResultVo sendOrder(@RequestBody GoodsOrder order){
        LambdaUpdateWrapper<GoodsOrder> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(GoodsOrder::getIsSend,order.getIsSend())
                .eq(GoodsOrder::getOrderId,order.getOrderId());
        goodsOrderService.update(wrapper);
        return ResultUtils.success("yes");
    }
}
